using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentFeeDetailDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentFeeDetail objects.
    /// </summary>
    public class StudentFeeDetailDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentFeeDetail from the underlying datasource.
        /// </summary>
        /// <param name="StudentFeeKey">The unique StudentFeeKey of the StudentFeeDetail in the database.</param>
        /// <returns>An StudentFeeDetail when the StudentFeeKey was found in the database, or null otherwise.</returns>
        public static StudentFeeDetail GetItem(long SFDKey, int StudentFeeKey)
        {
            StudentFeeDetail myStudentFeeDetail = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeDetailSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@SFDKey", SFDKey);
                    myCommand.Parameters.AddWithValue("@StudentFeeKey", StudentFeeKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentFeeDetail = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentFeeDetail;
            }
        }

        /// <summary>
        /// Returns a list with StudentFeeDetail objects.
        /// </summary>
        /// <returns>A generics List with the StudentFeeDetail objects.</returns>
        public static StudentFeeDetailList GetList()
        {
            StudentFeeDetailList tempList = new StudentFeeDetailList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeDetailSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentFeeDetail in the database.
        /// </summary>
        /// <param name="myStudentFeeDetail">The StudentFeeDetail instance to save.</param>
        /// <returns>The new StudentFeeKey if the StudentFeeDetail is new in the database or the existing StudentFeeKey when an item was updated.</returns>
        public static int Save(StudentFeeDetail myStudentFeeDetail)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeDetailInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentFeeDetail.SFDKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@SFDKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@SFDKey", myStudentFeeDetail.SFDKey);
                    }

                    if (myStudentFeeDetail.StudentFeeKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@StudentFeeKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@StudentFeeKey", myStudentFeeDetail.StudentFeeKey);
                    }
                    if (string.IsNullOrEmpty(myStudentFeeDetail.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myStudentFeeDetail.Description);
                    }
                    if (myStudentFeeDetail.Amount < 0)
                    {
                        myCommand.Parameters.AddWithValue("@amount", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@amount", myStudentFeeDetail.Amount);
                    }
                    if (myStudentFeeDetail.AdditionalAmount < 0)
                    {
                        myCommand.Parameters.AddWithValue("@additionalAmount", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@additionalAmount", myStudentFeeDetail.AdditionalAmount);
                    }
                    if (myStudentFeeDetail.OtherAmount < 0)
                    {
                        myCommand.Parameters.AddWithValue("@otherAmount", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@otherAmount", myStudentFeeDetail.OtherAmount);
                    }
                    if (myStudentFeeDetail.DueDate == null)
                    {
                        myCommand.Parameters.AddWithValue("@dueDate", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@dueDate", myStudentFeeDetail.DueDate);
                    }
                    if (myStudentFeeDetail.FinalDueDate == null)
                    {
                        myCommand.Parameters.AddWithValue("@finalDueDate", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@finalDueDate", myStudentFeeDetail.FinalDueDate);
                    }

                    myCommand.Parameters.AddWithValue("@status", myStudentFeeDetail.Status);

                    //if (myStudentFeeDetail.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentFeeDetail.CreatedDateTime);
                    //}
                    //if (myStudentFeeDetail.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentFeeDetail.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentFeeDetail from the database.
        /// </summary>
        /// <param name="StudentFeeKey">The StudentFeeKey of the StudentFeeDetail to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(Int64 SDFKey, int StudentFeeKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentFeeDetailDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@SFDKey", SDFKey);
                    myCommand.Parameters.AddWithValue("@StudentFeeKey", StudentFeeKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentFeeDetail class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentFeeDetail FillDataRecord(IDataRecord myDataRecord)
        {
            StudentFeeDetail myStudentFeeDetail = new StudentFeeDetail();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SFDKey")))
            {
                myStudentFeeDetail.SFDKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("SFDKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentFeeKey")))
            {
                myStudentFeeDetail.StudentFeeKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentFeeKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myStudentFeeDetail.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Amount")))
            {
                myStudentFeeDetail.Amount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Amount"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AdditionalAmount")))
            {
                myStudentFeeDetail.AdditionalAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("AdditionalAmount"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherAmount")))
            {
                myStudentFeeDetail.OtherAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("OtherAmount"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DueDate")))
            {
                myStudentFeeDetail.DueDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("DueDate"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FinalDueDate")))
            {
                myStudentFeeDetail.FinalDueDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("FinalDueDate"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentFeeDetail.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentFeeDetail.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentFeeDetail.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentFeeDetail;
        }
    }

}
